// Fill out your copyright notice in the Description page of Project Settings.


#include "SPakAssetRow.h"
#include "SlateOptMacros.h"
#include "SPakAssetsView.h"
#include "Widgets/Text/STextBlock.h"
#include "Widgets/Input/SCheckBox.h"
#include "HotPakerFileLib.h"


const FName SPakAssetRow::Col_AssetName(TEXT("AssetName"));
const FName SPakAssetRow::Col_GuidName(TEXT("GuidName"));
const FName SPakAssetRow::Col_ChunkName(TEXT("ChunkName"));

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SPakAssetRow::Construct(const FArguments& InArgs, SPakAssetsView* InPakAssetsView)
{
	PakAssetsView = InPakAssetsView;
	AssetDetailNode = InArgs._AssetDetailNode;

	SMultiColumnTableRow<FAssetDetailNode_Ref>::Construct(SMultiColumnTableRow<FAssetDetailNode_Ref>::FArguments().Padding(1), InArgs._OwnerTableView.ToSharedRef());

}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

TSharedRef<SWidget> SPakAssetRow::GenerateWidgetForColumn(const FName& ColumnName)
{
	if (ColumnName.IsEqual(Col_AssetName))
	{
		return SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
			.AutoWidth()
			.VAlign(VAlign_Center)
			[
				SNew(SExpanderArrow, SharedThis(this))
			]

		+ SHorizontalBox::Slot()
			.AutoWidth()
			[
				SNew(SCheckBox)
				.IsChecked(this, &SPakAssetRow::HandleCheckBoxIsChecked)
			.OnCheckStateChanged(this, &SPakAssetRow::HandleCheckBoxCheckStateChanged)
			.Padding(FMargin(6.0, 2.0))
			[
				SNew(STextBlock)
				.Text(FText::FromName(AssetDetailNode->PackageName))
			]
			];
	}
	else if (ColumnName.IsEqual(Col_GuidName))
	{
		return SNew(STextBlock)
			.Text(FText::FromString(AssetDetailNode->Guid));
	}
	else if (ColumnName.IsEqual(Col_ChunkName))
	{
		return SNew(STextBlock)
			.Text(FText::FromString(AssetDetailNode->Chunks));
	}

	return SNullWidget::NullWidget;
}

void SPakAssetRow::HandleCheckBoxCheckStateChanged(ECheckBoxState NewState)
{
	TArray<FName> DependentAssets;

	switch (HandleCheckBoxIsChecked())
	{
	case ECheckBoxState::Checked:
		//only uncheck state, turn to check multi or uncheck

		DependentAssets = UHotPakerFileLib::GetAssetDependencies(AssetDetailNode->PackageName);

		if (DependentAssets.Num() > 0)
			for (auto DependentAsset : DependentAssets)
				PakAssetsView->AddSelectAsset(DependentAsset);
		else
			PakAssetsView->RemoveSelectAsset(AssetDetailNode->PackageName);

		break;
	case ECheckBoxState::Unchecked:
		// only checked state, turn to check

		PakAssetsView->AddSelectAsset(AssetDetailNode->PackageName);

		break;
	case ECheckBoxState::Undetermined:
		// only unchecked state , turn to uncheck

		PakAssetsView->RemoveSelectAsset(AssetDetailNode->PackageName);
		DependentAssets = UHotPakerFileLib::GetAssetDependencies(AssetDetailNode->PackageName);
		for (auto DependentAsset : DependentAssets)
			PakAssetsView->RemoveSelectAsset(DependentAsset);

		break;
	}
}

ECheckBoxState SPakAssetRow::HandleCheckBoxIsChecked() const
{
	if (PakAssetsView->IsDependenceSelected(AssetDetailNode->PackageName))
		return ECheckBoxState::Undetermined;
	if (PakAssetsView->GetAllSelectAssets().Contains(AssetDetailNode->PackageName))
		return ECheckBoxState::Checked;
	return ECheckBoxState::Unchecked;
}



